package com.example.mobilesafe.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.example.mobilesafe.R;
import com.example.mobilesafe.dao.CommonnumberDao;
import com.example.mobilesafe.domain.ChildInfo;
import com.example.mobilesafe.domain.GroupInfo;

import java.util.List;

public class CommonNumberQueryActivity extends AppCompatActivity {

    private ExpandableListView elv_common_number;

    private List<GroupInfo> mGroup;

    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_number_query);

        // 初始化UI
        initUI();

        // 初始化数据
        initData();
    }

    /**
     * 初始化UI
     */
    private void initUI() {
        elv_common_number = findViewById(R.id.elv_common_number);
    }

    /**
     * 给可扩展的ListView准备数据，进行填充
     */
    private void initData() {
        CommonnumberDao commonnumberDao = new CommonnumberDao();
        mGroup = commonnumberDao.getGroup();
        mAdapter = new MyAdapter();
        elv_common_number.setAdapter(mAdapter);
        // 给可扩展的ExpandableListView注册点击事件（打电话）
        elv_common_number.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                startCall(mAdapter.getChild(groupPosition,childPosition).getNumber());
                return false;
            }
        });
    }

    /**
     * 打电话功能封装
     * @param number 列表中对应的孩子节点的条目中的电话
     */
    private void startCall(String number) {
        // 开启系统的打电话界面
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:"+ number));
        startActivity(intent);
    }

    class MyAdapter extends BaseExpandableListAdapter {

        @Override
        public int getGroupCount() {
            return mGroup.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return mGroup.get(groupPosition).getChildList().size();
        }

        @Override
        public GroupInfo getGroup(int groupPosition) {
            return mGroup.get(groupPosition);
        }

        @Override
        public ChildInfo getChild(int groupPosition, int childPosition) {
            return mGroup.get(groupPosition).getChildList().get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            TextView textView = new TextView(getApplicationContext());
            textView.setText(getGroup(groupPosition).getName());
            textView.setTextColor(Color.RED);
            // textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,20); 字体大小
            return textView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            View view = View.inflate(getApplicationContext(), R.layout.elv_child_item, null);
            TextView tv_name = view.findViewById(R.id.tv_name);
            TextView tv_number = view.findViewById(R.id.tv_number);
            tv_name.setText(getChild(groupPosition,childPosition).getName());
            tv_number.setText(getChild(groupPosition,childPosition).getNumber());
            return view;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            // 孩子节点中的条目是否相应事件
            return true;
        }
    }
}